<?xml version="1.0" encoding="UTF-8" ?>

<!-- One XSLT that use HTML + CSS to display all the relevant information about the games of a specific week, 
with a parameter that identify the requested week. -->

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
	<xsl:output method="xml" indent="yes"/>
	<xsl:strip-space elements="*"/>
	
	<xsl:template match="/">
		<xsl:processing-instruction name="xml-stylesheet">type="text/xsl" href="xslt/fixture_page.xslt"</xsl:processing-instruction>
		
		<league_fixtures>
			
		<xsl:for-each select="league/fixtures">
			<xsl:variable name="fixtureId" select="@id"/>
			<fixture>
					<xsl:attribute name="id"><xsl:value-of select="$fixtureId"/></xsl:attribute>				
				
					<xsl:for-each select="game">
						<game>
							<xsl:variable name="gameId" select="@id"/>
							<xsl:variable name="homeId" select="home_team/@teamId"/>
							<xsl:variable name="awayId" select="visitor_team/@teamId"/>
								<id><xsl:value-of select="$gameId" /></id>
								<home_team>
									<xsl:attribute name="goals"><xsl:value-of select="score/home"/></xsl:attribute>
									<xsl:attribute name="name"><xsl:value-of select="/league/teams/team[@id=$homeId]/acronym"></xsl:value-of></xsl:attribute>
									<xsl:value-of select='substring-after(home_team/@teamId,"_")'/>
								</home_team>
								<visitor_team>
									<xsl:attribute name="goals"><xsl:value-of select="score/visitor"/></xsl:attribute>
									<xsl:attribute name="name"><xsl:value-of select="/league/teams/team[@id=$awayId]/acronym"/></xsl:attribute>
									<xsl:value-of select='substring-after(visitor_team/@teamId,"_")'/>
								</visitor_team>
								<date><xsl:value-of select="game_date" /></date>
								<attendance><xsl:value-of select="attendance" /></attendance>	


					
								
								<lineup>
									<xsl:attribute name="teamId"><xsl:value-of select="$homeId"/></xsl:attribute>
									<xsl:call-template name="playerProcessor">
										<xsl:with-param name="playerList" select="lineup/game_team[@teamId=$homeId]/initial/@playersId"/>
										<xsl:with-param name="teamId" select="$homeId"/>
										<xsl:with-param name="fixtureId" select="$fixtureId"/>
										<xsl:with-param name="gameId" select="$gameId"/>								
									</xsl:call-template>									
								</lineup>
								
								<lineup>								
									<xsl:attribute name="teamId"><xsl:value-of select="$awayId"/></xsl:attribute>
									<xsl:call-template name="playerProcessor">
										<xsl:with-param name="playerList" select="lineup/game_team[@teamId=$awayId]/initial/@playersId"/>
										<xsl:with-param name="teamId" select="$awayId"/>
										<xsl:with-param name="fixtureId" select="$fixtureId"/>
										<xsl:with-param name="gameId" select="$gameId"/>
									</xsl:call-template>
								</lineup>
								
								<substitutes>
									<xsl:attribute name="teamId"><xsl:value-of select="$homeId"/></xsl:attribute>
									<xsl:call-template name="playerProcessor">
										<xsl:with-param name="playerList" select="lineup/game_team[@teamId=$homeId]/substitute/@playersId"/>
										<xsl:with-param name="teamId" select="$homeId"/>
										<xsl:with-param name="fixtureId" select="$fixtureId"/>
										<xsl:with-param name="gameId" select="$gameId"/>
									</xsl:call-template>
								</substitutes>
								
									
								<substitutes>
									<xsl:attribute name="teamId"><xsl:value-of select="$awayId"/></xsl:attribute>
									<xsl:call-template name="playerProcessor">
										<xsl:with-param name="playerList" select="lineup/game_team[@teamId=$awayId]/substitute/@playersId"/>
										<xsl:with-param name="teamId" select="$awayId"/>
										<xsl:with-param name="fixtureId" select="$fixtureId"/>
										<xsl:with-param name="gameId" select="$gameId"/>
									</xsl:call-template>
								</substitutes>
								
								
								
							</game>		
						</xsl:for-each>
			</fixture>
		</xsl:for-each>
		
		
		
		
		
		
		</league_fixtures>
	</xsl:template>

<xsl:template name="playerProcessor">
<xsl:param name="playerList" />
<xsl:param name="teamId" />
<xsl:param name="fixtureId" />
<xsl:param name="gameId" />
	<xsl:for-each select="id($playerList)">
		<player>			
			<xsl:variable name="playerId" select="@id"/>
			<xsl:attribute name="id" select="@id"/>
			
			<name><xsl:value-of select="nickname"/></name>
			
			<shirt><xsl:value-of select="/league/teams/team[@id=$teamId]/squad/squad_player[@playerId=$playerId]/number"/></shirt>									
			<position><xsl:value-of select="/league/players/player[@id=$playerId]/@position"/></position>									
			
			<xsl:for-each select="/league/fixtures[@id=$fixtureId]/game[@id=$gameId]/score/goals/goal[@playerId= $playerId]">
				<goal><xsl:value-of select="minute"/></goal>
			</xsl:for-each>
			
			<xsl:for-each select="/league/fixtures[@id=$fixtureId]/game[@id=$gameId]/discipline/card[@playerId= $playerId]">
				<card>
					<xsl:attribute name="color"><xsl:value-of select="cardtype/@color"/></xsl:attribute>
					<xsl:value-of select="minute"/>
				</card>
			</xsl:for-each>
			
			<xsl:for-each select="/league/fixtures[@id=$fixtureId]/game[@id=$gameId]/substitutions/substitution/out[@playerId= $playerId]">
				<out><xsl:value-of select="./../minute"/></out>
			</xsl:for-each>		
			
			<xsl:for-each select="/league/fixtures[@id=$fixtureId]/game[@id=$gameId]/substitutions/substitution/in[@playerId= $playerId]">
				<in><xsl:value-of select="./../minute"/></in>
			</xsl:for-each>		
				 
		</player>
	</xsl:for-each>											
</xsl:template>

</xsl:stylesheet>
